﻿using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;

namespace INdTFramework.Controls
{
    /// <summary>
    /// This control allows applications autheticate your users using their
    /// facebook login credentials. Once button is clicked the client is
    /// redirect to facebook web login page to input your data. At the end
    /// the controls return the user data via navigate parameters to RedirectURI
    /// informed on properties.
    /// </summary>
    public partial class FacebookLoginButton : UserControl
    {
        private const String BaseUri = "/INdTFramework;component/Controls/FacebookButtonView.xaml?appid={0}&secret={1}&scope={2}&destination={3}&clearcookies={4}";

        /// <summary>
        /// Valid Facebook application id
        /// </summary>
        public string AppId { get; set; }

        /// <summary>
        /// Valid facebook application secret
        /// </summary>
        public string Secret { get; set; }

        /// <summary>
        /// Scope of data required by application. e.g. email, friend list, albums.
        /// </summary>
        public string Scope { get; set; }

        /// <summary>
        /// Local aplication xaml to where use should be redirect in case of succesful
        /// login.
        /// </summary>
        public string RedirectURI { get; set; }

        /// <summary>
        /// Define if the WebBrowser compontent should delete or not previously created
        /// cookies. Not delete cookies save your crendentials at isolated storage to the
        /// next login, so you don't need retype them all times, but can lead application
        /// to a security issue.
        /// </summary>
        public bool ClearCookies { get; set; }

        public FacebookLoginButton()
        {
            InitializeComponent();
            ClearCookies = true;
        }

        private void ButtonClicked(object sender, RoutedEventArgs e)
        {
            string UriFormatted = String.Format(BaseUri, AppId, Secret, Scope, RedirectURI, ClearCookies.ToString());

            ((PhoneApplicationFrame)Application
                .Current.RootVisual).Navigate(new Uri(UriFormatted, UriKind.Relative));
        }
    }
}
